package com.youlai.boot.modules.resource.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.resource.converter.ResourceConverter;
import com.youlai.boot.modules.resource.mapper.ResourceMapper;
import com.youlai.boot.modules.resource.model.Resource;
import com.youlai.boot.modules.resource.model.ResourceForm;
import com.youlai.boot.modules.resource.model.ResourceVO;
import com.youlai.boot.modules.resource.model.ResourcePageQuery;
import com.youlai.boot.modules.resource.service.ResourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {

    private final ResourceConverter resourceConverter;

    @Override
    public Page<ResourceVO> getResourcePage(ResourcePageQuery query) {
        Page<ResourceVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return baseMapper.getResourcePage(page, query);
    }

    @Override
    public ResourceVO getResourceById(Long id) {
        Resource resource = this.getById(id);
        return resourceConverter.toVO(resource);
    }

    @Override
    public Long saveResource(ResourceForm form) {
        Resource resource = resourceConverter.toEntity(form);
        this.save(resource);
        return resource.getId();
    }

    @Override
    public void updateResource(ResourceForm form) {
        Resource resource = resourceConverter.toEntity(form);
        this.updateById(resource);
    }

    @Override
    public boolean deleteResource(Long id) {
        return this.removeById(id);
    }
}